//
//  DetailAlbumsViewController.m
//  AudioBook_A
//
//  Created by Sansan on 15/4/16.
//  Copyright (c) 2015年 Sansan. All rights reserved.
//

#import "DetailAlbumsViewController.h"

@interface DetailAlbumsViewController ()

@end

@implementation DetailAlbumsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.title;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    
    self.DetalTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    
    self.DetalTableView.delegate = self;
    self.DetalTableView.dataSource = self;
    
    self.DetalTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:self.DetalTableView];
    
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenFram.width, ScreenFram.height/5.0)];
    aView.backgroundColor = [UIColor colorWithRed:191/255.0 green:230/255.0 blue:240/255.0 alpha:1.0];
    // 设置表头视图
    self.DetalTableView.tableHeaderView = aView;
    
    
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, ScreenFram.width/4, ScreenFram.width/4)];
    [aView addSubview:self.headImageView];
    
    
    self.allModelArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:URLDETAL, self.ID, 1];
    [MyAFNData getNetData:url responseGETData:^(id data) {
        NSMutableDictionary *dictionary = [data objectForKey:@"tracks"];
        NSMutableArray *array = [dictionary objectForKey:@"list"];
        NSMutableDictionary *albumDic = [data objectForKey:@"album"];
        DetailAlbumsModel *albumModel = [[DetailAlbumsModel alloc] init];
        [albumModel setValuesForKeysWithDictionary:albumDic];
        // 设置head 图片
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:albumModel.coverOrigin] placeholderImage:[UIImage imageNamed:@"bg_albumView_header.png"]];
        
        // 设置小头像的nickname
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenFram.width/3, 20, ScreenFram.width/1.7, 30)];
        titleLabel.text = albumModel.title;
        titleLabel.font = [UIFont systemFontOfSize:20];
        [aView addSubview:titleLabel];
        
        // 设置introButton
        UIButton *introButton = [UIButton buttonWithType:UIButtonTypeCustom];
        introButton.frame = CGRectMake(ScreenFram.width/3, ScreenFram.height/11.0, ScreenFram.width/2, 20);
        introButton.titleLabel.font = [UIFont systemFontOfSize:16];
        introButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        [introButton setTitle:albumModel.intro forState:UIControlStateNormal];
        [introButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [introButton addTarget:self action:@selector(introButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [aView addSubview:introButton];
        
        // 设置简介的View图标
        UIImageView *introImageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenFram.width/2.6+ScreenFram.width/2.0, ScreenFram.height/11.0, 20, 20)];
         introImageView.image = [UIImage imageNamed:@"iconfont-jiantou.png"];
        [aView addSubview:introImageView];
        
        // 设置tagsLabel
        UILabel *tagsLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenFram.width/3, ScreenFram.height/7.0, ScreenFram.width/2, 20)];
        tagsLabel.text = albumModel.tags;
        tagsLabel.font = [UIFont systemFontOfSize:12];
        [aView addSubview:tagsLabel];
        
        // 设置下载的button
        UIButton *allDownLoad = [UIButton buttonWithType:UIButtonTypeCustom];
        allDownLoad.frame = CGRectMake(ScreenFram.width/2.7+ScreenFram.width/2.0, ScreenFram.height/7.0, 28, 24);
        [allDownLoad setImage:[UIImage imageNamed:@"iconfont-xiazai.png"] forState:UIControlStateNormal];
        [allDownLoad addTarget:self action:@selector(allDownLoadAction) forControlEvents:UIControlEventTouchUpInside];
        [aView addSubview:allDownLoad];
        
        for (NSMutableDictionary *dic in array) {
            
            TracksModel *model = [[TracksModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allModelArray addObject:model];
            
        }
        
        [self.DetalTableView reloadData];
        
    }];
    
    self.page = 1;
    
    // 2.集成刷新控件
    [self addFooter];

}

// 下载全集的button触发方法
- (void)allDownLoadAction
{
    NSLog(@"下载全集的触发方法");
}


// share分享按钮触发的方法
- (void)shareAction
{
    NSLog(@"分享触发的方法");
}

// introButton触发的方法
- (void)introButtonAction
{
    NSLog(@"触发简介方法");
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

// 两个必须实现的方法之一
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allModelArray.count;
}

// 两个必须实现的方法之一
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInterller = @"cell";
    DetailAlbumsTableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellInterller];
    if (cell == nil) {
        
        cell = [[DetailAlbumsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellInterller];
    }
    // cell 点击后没有阴影效果
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    TracksModel *model = [self.allModelArray objectAtIndex:indexPath.row];
    cell.model = model;
    cell.contentView.backgroundColor = [UIColor colorWithRed:200/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
    
    return cell;
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block DetailAlbumsViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.DetalTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        NSLog(@"上拉加载更多，开始网络请求更多数据");
        
        
        // 用afn加载更多数据
        NSString *url = [NSString stringWithFormat:URLDETAL, self.ID, (int)++self.page];
        [MyAFNData getNetData:url responseGETData:^(id data) {
            NSMutableDictionary *dictionary = [data objectForKey:@"tracks"];
            NSMutableArray *array = [dictionary objectForKey:@"list"];
            for (NSMutableDictionary *dic in array) {
                
                TracksModel *model = [[TracksModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.allModelArray addObject:model];
                
            }
            
            [vc.DetalTableView footerEndRefreshing];
            [self.DetalTableView reloadData];
            
        }];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [AudioPlayerHandle shareHandle].isCellSelected = YES;
    TracksModel *myCellModel = (TracksModel *)[self.allModelArray objectAtIndex:indexPath.row];
    [AudioPlayerHandle shareHandle].audioArray = [NSMutableArray array];
    [AudioPlayerHandle shareHandle].audioArray = self.allModelArray;
    [AudioPlayerHandle shareHandle].audioIndex = indexPath.row;
    [[AudioPlayerHandle shareHandle] createStreamer:myCellModel.playUrl64 duration:myCellModel.duration];
    [AudioPlayerHandle shareHandle].currentUrl = myCellModel.playUrl64;
    [[AudioPlayerHandle shareHandle].audioStream start];
    
    [[AudioPlayerHandle shareHandle].playButton setBackgroundImage:[UIImage imageNamed:@"pause_32.png"] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
